﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Meqs.Entity;
using System.Collections.Generic;
using Meqs.Logic.Interface;

public partial class Base_School_uploadLeader : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnReqClsOK_ServerClick(object sender, EventArgs e)
    {
        if (FileUpload1.HasFile)
        {
            try
            {
                SchoolInfo si = SchoolCommon.Sch_SchoolInfo;
                Hashtable ht = new Hashtable();
                ht.Add("Roleses", "6 and 8");
                ht.Add("SchoolYear", si.SchoolYear);
                ht.Add("Term", si.SchoolTerm);
                Container<ITeacherRolesLogic>.Instance.DeleteBy(ht);

                ht.Clear();
                ht.Add("Iseffect", 1);
                IList<GlobalCauseInfo> gcis = Container<IGlobalCauseInfoLogic>.Instance.FindAll(ht);

                DataSet ds = Meqs.Utilities.ExcelUtil.ExcelImport(FileUpload1.PostedFile.InputStream);
                int rowindex = 0;
                if (ds.Tables.Count > 0)
                {
                    foreach (DataRow row in ds.Tables[0].Rows)
                    {
                        GlobalCauseInfo g = null;
                        for (int i = 0; i < ds.Tables[0].Columns.Count; i++)
                        {
                            if (rowindex == 0)
                            {
                                //级长
                                if (!(row[i] is DBNull) && i > 1)
                                {
                                    string tercher = row[i].ToString().Replace(" ", "").ToLower();
                                    ht.Clear();
                                    ht.Add("FullTeachName", tercher);
                                    ht.Add("IsEffect", 1);
                                    IList<TeacherInfo> tis = Container<ITeacherInfoLogic>.Instance.FindAll(ht);
                                    if (tis.Count > 0)
                                    {
                                        int grade = 7;
                                        if (i == 3) { grade = 8; }
                                        if (i == 4) { grade = 9; }
                                        TeacherRoles t = new TeacherRoles();
                                        t.RoleID = (int)RoleKeyEnum.Grade_Director;
                                        t.SchoolMark = si.SchoolMark;
                                        t.SchoolYear = si.SchoolYear;
                                        t.TeacherID = tis[0].RID;
                                        t.Term = si.SchoolTerm;
                                        t.GreadID = grade;
                                        t.CauseID = 0;
                                        Container<ITeacherRolesLogic>.Instance.Create(t);
                                    }
                                }
                            }
                            else
                            { 
                                //科长，备课组长
                                if (!(row[i] is DBNull) && i == 0)
                                {
                                    foreach (GlobalCauseInfo gg in gcis)
                                    {
                                        if (gg.CauseName == row[i].ToString().Replace(" ", ""))
                                        {
                                            g = gg;
                                            break;
                                        }
                                    }

                                }
                                if (!(row[i] is DBNull) && i > 0)
                                {

                                    string tercher = row[i].ToString().Replace(" ", "").ToLower();
                                    ht.Clear();
                                    ht.Add("FullTeachName", tercher);
                                    ht.Add("IsEffect", 1);
                                    IList<TeacherInfo> tis = Container<ITeacherInfoLogic>.Instance.FindAll(ht);
                                    if (tis.Count > 0)
                                    {
                                        if (i == 1)
                                        {
                                            TeacherRoles t = new TeacherRoles();
                                            t.RoleID = (int)RoleKeyEnum.Subject_Director;
                                            t.SchoolMark = si.SchoolMark;
                                            t.SchoolYear = si.SchoolYear;
                                            t.TeacherID = tis[0].RID;
                                            t.Term = si.SchoolTerm;
                                            t.GreadID = 0;
                                            t.CauseID = g.RID;
                                            Container<ITeacherRolesLogic>.Instance.Create(t);
                                        }
                                        else
                                        {
                                            int grade = 7;
                                            if (i == 3) { grade = 8; }
                                            if (i == 4) { grade = 9; }
                                            TeacherRoles t = new TeacherRoles();
                                            t.RoleID = (int)RoleKeyEnum.GradeSubject_Director;
                                            t.SchoolMark = si.SchoolMark;
                                            t.SchoolYear = si.SchoolYear;
                                            t.TeacherID = tis[0].RID;
                                            t.Term = si.SchoolTerm;
                                            t.GreadID = grade;
                                            t.CauseID = g.RID;
                                            Container<ITeacherRolesLogic>.Instance.Create(t);
                                        }
                                    }

                                }
                            }
                        }
                        rowindex++;
                    }
                    Response.Write("<script>parent.HaveDone(1,'')</script>");
                }
                else
                {
                    //错误文件
                    Response.Write("<script>parent.HaveDone(2,'无法获取数据，请使用模版进行填报！')</script>");
                }
            }
            catch (Exception ex)  { Response.Write("<script>parent.HaveDone(2,'" + ex.Message + "')</script>"); }
        }
        else
        {
            Response.Write("<script>parent.HaveDone(2,'文件错误！')</script>");
        }
    }

}
